package com.aiju.common.event;

import com.aiju.support.lifecycle.LifecycleListener;
import com.aiju.utils.L;

public class EventBusHandler implements LifecycleListener {

    private boolean mRegistered = false;

    public EventBusHandler register() {
        if (!mRegistered) {
            mRegistered = true;
            EventCenter.getInstance().register(this);
            L.d("register");
        }
        return this;
    }

    public EventBusHandler unregister() {
        if (mRegistered) {
            mRegistered = false;
            EventCenter.getInstance().unregister(this);
            L.d("unregister");
        }
        return this;
    }

    public synchronized void tryToRegisterIfNot() {
        register();
    }

    @Override
    public void onStart() {
        register();
    }

    @Override
    public void onStop() {

    }

    @Override
    public void onDestroy() {
        unregister();
    }

}
